package com.ruoyi.basedata.service.impl;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.ruoyi.basedata.domain.vo.UserTerminalsxbJoinBo;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.basedata.mapper.UserTerminalsxbJoinMapper;
import com.ruoyi.basedata.domain.UserTerminalsxbJoin;
import com.ruoyi.basedata.service.IUserTerminalsxbJoinService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
/**
 * 用户配置Service业务层处理
 *
 * @author gdqt
 * @date 2025-07-09
 */
@Service
public class UserTerminalsxbJoinServiceImpl extends ServiceImpl<UserTerminalsxbJoinMapper, UserTerminalsxbJoin> implements IUserTerminalsxbJoinService
{
    @Autowired
    private UserTerminalsxbJoinMapper userTerminalsxbJoinMapper;

    /**
     * 查询用户配置
     *
     * @param id 用户配置主键
     * @return 用户配置
     */
    @Override
    public UserTerminalsxbJoin selectUserTerminalsxbJoinById(Long id)
    {

        return userTerminalsxbJoinMapper.selectById(id);
    }

    /**
     * 查询用户配置列表
     *
     * @param userTerminalsxbJoin 用户配置
     * @return 用户配置
     */
    @Override
    public List<UserTerminalsxbJoin> selectUserTerminalsxbJoinList(UserTerminalsxbJoin userTerminalsxbJoin)
    {
        LambdaQueryWrapper<UserTerminalsxbJoin> queryWrapper = new LambdaQueryWrapper<>();
        if(!StringUtils.isEmpty(userTerminalsxbJoin.getTenantId())){
            queryWrapper.eq(UserTerminalsxbJoin::getTenantId,userTerminalsxbJoin.getTenantId());
        }
        if(!StringUtils.isEmpty(userTerminalsxbJoin.getUserId())){
            queryWrapper.eq(UserTerminalsxbJoin::getUserId,userTerminalsxbJoin.getUserId());
        }
        if(userTerminalsxbJoin.getEnergyType() != null){
            queryWrapper.eq(UserTerminalsxbJoin::getEnergyType,userTerminalsxbJoin.getEnergyType());
        }
        if(!StringUtils.isEmpty(userTerminalsxbJoin.getLocationNb())){
            queryWrapper.eq(UserTerminalsxbJoin::getLocationNb,userTerminalsxbJoin.getLocationNb());
        }
        if(!StringUtils.isEmpty(userTerminalsxbJoin.getTerminalId())){
            queryWrapper.eq(UserTerminalsxbJoin::getTerminalId,userTerminalsxbJoin.getTerminalId());
        }
        if(userTerminalsxbJoin.getCalcType() != null){
            queryWrapper.eq(UserTerminalsxbJoin::getCalcType,userTerminalsxbJoin.getCalcType());
        }
        if(userTerminalsxbJoin.getCalcRate() != null){
            queryWrapper.eq(UserTerminalsxbJoin::getCalcRate,userTerminalsxbJoin.getCalcRate());
        }
        if(userTerminalsxbJoin.getStatus() != null){
            queryWrapper.eq(UserTerminalsxbJoin::getStatus,userTerminalsxbJoin.getStatus());
        }else{
            queryWrapper.in(UserTerminalsxbJoin::getStatus,1,2);
        }
        return userTerminalsxbJoinMapper.selectList(queryWrapper);
    }


    @Override
    public List<UserTerminalsxbJoinBo> selectUserTerminalsxbJoinListMore(UserTerminalsxbJoin userTerminalsxbJoin) {

        return userTerminalsxbJoinMapper.selectListMore(userTerminalsxbJoin);
    }

    /**
     * 新增用户配置
     *
     * @param userTerminalsxbJoin 用户配置
     * @return 结果
     */
    @Override
    public int insertUserTerminalsxbJoin(UserTerminalsxbJoin userTerminalsxbJoin)
    {
        return userTerminalsxbJoinMapper.insert(userTerminalsxbJoin);
    }

    @Override
    public int cloneUserTerminalsxbJoin(String oldUserId, String newUserId)
    {
        return userTerminalsxbJoinMapper.cloneinsert(oldUserId,newUserId);
    }

    /**
     * 修改用户配置
     *
     * @param userTerminalsxbJoin 用户配置
     * @return 结果
     */
    @Override
    public int updateUserTerminalsxbJoin(UserTerminalsxbJoin userTerminalsxbJoin)
    {
        userTerminalsxbJoin.setUpdateTime(DateUtils.getNowDate());
        return userTerminalsxbJoinMapper.updateById(userTerminalsxbJoin);
    }

    /**
     * 批量删除用户配置
     *
     * @param ids 需要删除的用户配置主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteUserTerminalsxbJoinByIds(Long[] ids)
    {
        for (Long id : ids
                ) {
            UserTerminalsxbJoin userTerminalsxbJoin = userTerminalsxbJoinMapper.selectById(id);
            if (userTerminalsxbJoin == null) {
                continue;
            }
            userTerminalsxbJoin.setStatus(3);
            int update = userTerminalsxbJoinMapper.updateById(userTerminalsxbJoin);
            if (update < 1){
                throw new RuntimeException("删除用户配置失败");
            }
        }
        return ids.length;

    }

    @Override
    public void deleteUserTerminalsxbJoinByUser(String userId)
    {
        userTerminalsxbJoinMapper.deleteUserTerminalsxbJoinByUser(userId);

    }

    /**
     * 删除用户配置信息
     *
     * @param id 用户配置主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteUserTerminalsxbJoinById(Long id)
    {
        return userTerminalsxbJoinMapper.deleteById(id);
    }
}
